package com.example.service.impl;

import com.common.result.Result;
import com.common.utils.JwtUtil;
import com.common.utils.ThreadLocalUtil;
import com.example.interceptor.JwtTokenInterceptor;
import com.example.mapper.EmployeeMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pojo.dto.EmployeeDTO;
import com.pojo.dto.EmployeeLoginDTO;
import com.pojo.dto.QueryPageDTO;
import com.pojo.entity.Employee;
import com.example.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.security.auth.login.LoginException;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class EmployeeServiceImpl implements EmployeeService {



    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        employee.setPassword(DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()));
/*        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser(ThreadLocalUtil.get());
        employee.setUpdateUser(ThreadLocalUtil.get());*/
        employee.setStatus(1);
        employeeMapper.save(employee);
    }

    @Override
    public Map<String,Object> login(EmployeeLoginDTO employeeLoginDTO) throws LoginException {
        System.out.println("------------------------employeeLoginDTO = " + employeeLoginDTO);
        if (employeeLoginDTO.getUsername() == null || employeeLoginDTO.getPassword() == null || employeeLoginDTO.getUsername() == "" || employeeLoginDTO.getPassword() == ""){
            throw new LoginException("账号密码不能为空");
        }

        Employee employee = employeeMapper.getByUsername(employeeLoginDTO.getUsername());

        if (employee == null){
            throw new LoginException("账号不存在");
        }

        if (!employee.getPassword().equals(DigestUtils.md5DigestAsHex(employeeLoginDTO.getPassword().getBytes()))){
            throw new LoginException("密码错误");
        }

        if (employee.getStatus() == 0){
            throw new LoginException("账号已禁用");
        }

        Map<String,Object> loginMap = new HashMap<>();
        loginMap.put("username",employee.getUsername());
        loginMap.put("name",employee.getName());
        loginMap.put("id",employee.getId());
        //生成token
        String token = JwtUtil.buildJwt(loginMap);
        loginMap.put("token",token);

        return loginMap;
    }

    @Override
    public List<Employee> allList() {
        List<Map<String, Object>> list = employeeMapper.allList();
        List<Employee> employees = new ArrayList<>();
        for (Map<String, Object> map : list){
            Employee employee = new Employee();
            employee.setId(Long.valueOf(map.get("id").toString()));
            employee.setName(map.get("name").toString());
            employee.setUsername(map.get("username").toString());
            employee.setPhone(map.get("phone").toString());
            employee.setSex(map.get("sex").toString());
            employee.setIdNumber(map.get("id_number").toString());
            employee.setStatus(Integer.valueOf(map.get("status").toString()));
            employee.setCreateTime(LocalDateTime.parse(map.get("create_time").toString()));
            employee.setUpdateTime(LocalDateTime.parse(map.get("update_time").toString()));
            employee.setCreateUser(Long.valueOf(map.get("create_user").toString()));
            employee.setUpdateUser(Long.valueOf(map.get("update_user").toString()));

            employees.add(employee);
        }
            return employees;
    }

    @Override
    public Page<Employee> queryPage(QueryPageDTO queryPageDTO) {
        PageHelper.startPage(queryPageDTO.getPageNum(), queryPageDTO.getPageSize());
        Page<Employee> page = employeeMapper.queryPage(queryPageDTO);

        return page;
    }

    @Override
    public Employee getById(Long id) {
        return employeeMapper.getById(id);
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {

        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
/*        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(ThreadLocalUtil.get());*/
         employeeMapper.update(employee);
    }

    @Override
    public void deleteById(Long id) {
        employeeMapper.deleteById(id);
    }
}











